package wynu.springbootlearn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wynu.springbootlearn.entity.Student;
import wynu.springbootlearn.entity.StudentAccount;
import wynu.springbootlearn.mapper.StudentAccountMapper;

// RestController = RequestBody + Controller:
// 意思是：1.该类是一个控制器类，控制器类下面的方法可以被路径匹配到；
// 2.该类可以将java对象转换为json对象返回给前端
@RestController
// RequestMapping：这个控制器下所有方法都是以"/student"路径开头的
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private Student student;

    @Autowired
    StudentAccountMapper studentAccountMapper;

    // 匹配GET请求，路径未填就沿用"/student"这个路径
    @GetMapping
    public Student getStudent() {
        return student;
    }

    // 匹配PUT请求，路径沿用"/student"这个路径，
    // 但是这里接收到了一个（路径）参数"{studentId}"，这个参数可以往下传递
    // @PathVariable：将路径中的茶树绑定到下面函数的参数列表中
    @PutMapping("{studentId}")
    public Student putStudent(@PathVariable Integer studentId) {

        String s = "修改学生：" + studentId;
        System.out.println(s);
        return new Student("张三", 21);
    }

    @PostMapping
    public Student addStudent(@RequestBody Student student) {
        System.out.println("收到了前端的Post请求，请求体是：" + student);
        System.out.println("hello");
        return student;
    }

    @DeleteMapping("/{studentId}")
    public Student deleteStudent(@PathVariable Integer studentId) {
        System.out.println("收到了前端的删除请求，id为：" + studentId);
        return new Student("张三", 21);
    }

    @GetMapping("/login")
    public StudentAccount login(String userName) {
        return studentAccountMapper.getStudentAccount(userName);
    }
}
